﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;
using System.IO;
namespace WebHousingInfo
{
    public partial class Home : System.Web.UI.Page
    {
        private static string modesave = null, modesavecomplete = null;
        NewsSaveBO newsaveBO;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //tu dong cap nhat tin nhan sms tu tong dai
                if (OptionBO.GetNameOptionBO("tudongcapnhatsms").OptionValue == "True")
                {
                    VipHouseBOList.GetNewsHouse_FromSMS(DateTime.Now);
                }
                //-----------------------------------------
                modesave = Request.QueryString["save"];
                modesavecomplete = Request.QueryString["action"];
                if (modesavecomplete == "complete")
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin !')", true);
                }
                if (modesave != null)
                {
                    if (Session["UserName"] == null)
                        Response.Redirect("DangNhap.aspx");
                    if (NewsSaveBOList.CheckVip_Exits(Convert.ToInt32(modesave)) == false)
                    {
                        newsaveBO = NewsSaveBO.NewNewsSaveBO();
                        newsaveBO.NewsHouseID = Convert.ToInt32(modesave);
                        newsaveBO.UserName = Session["UserName"].ToString();
                        try
                        {
                            newsaveBO.Save();
                            Response.Redirect("Ketquatimkiem.aspx?action=complete");
                        }
                        catch { }
                    }
                    else ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Đã lưu tin này !')", true);
                }
            }
        }

        public bool checkurlimage(string imagestring)
        {
            var pathFileImage = Server.MapPath("TotalUpload/ImagesTotal/" + imagestring);
            if (File.Exists(pathFileImage))
            {
                return true;
            }
            else return false;
        }

        public NewsHouseBOList Getdatasource(int cateid)
        {
            NewsHouseBOList newsboList;
            int soluongtin = Convert.ToInt32(OptionBO.GetNameOptionBO("soluongtinnhadattrangchu").OptionValue);
            if (cateid == 1)
            {
                //can ban
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num("ngaydang", soluongtin, "1");
                return newsboList;
            }
            else if (cateid == 6)
            {
                //can mua
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num("ngaydang", soluongtin, "6");
                return newsboList;
            }
            else if (cateid == 18)
            {
                //can thue
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num("ngaydang", soluongtin, "18");
                return newsboList;
            }
            else if (cateid == 19)
            {
                //can cho thue
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num("ngaydang", soluongtin, "19");
                return newsboList;
            }
            else
            {
                newsboList = NewsHouseBOList.GetAllNewsHouse_BySort_Num("ngaydang", soluongtin, "");
                return newsboList;
            }
        }
    }
}
